<?php

namespace App\Services\Article\Article;


use App\Models\Article\ArticleClassify;
use App\Services\Manage\ManageIndex;

/**
 * 文章分类逻辑层
 * Class ArticleclassifyLogic
 * @package App\Services\Article\Article
 */
class ArticleclassifyLogic
{

    /**
     * 根据id获取其父类的ids数组
     */
    public function getParentIds($id): array
    {
        $list = ArticleClassify::where('status','<>',-1)->get();
        $ids     = $id;
        $params  = 'id';
        $parentIds = parent_ids($list,$ids,$params,$id,0,'pid','id','');
        $ids = explode(",", $ids);
        foreach ($ids as $k => &$v) {
            $v = (int)$v;
        }
        array_splice($ids, array_search(0, $ids), 1);
        return array_reverse($ids);
    }

    /**
     * 文章分类添加
     * @return array
     */
    public function classifyAdd($data)
    {
        $data['create_time']=time();
        $data['update_time']=time();
        $rst = ArticleClassify::insertGetId($data);

        if ($rst === false) {
            return auto_failure ($data,'文章分类添加失败！');
        } elseif ($rst === 0) {
            return auto_failure ($data,'文章分类未作改动！');
        }

        return auto_return(['id' => $rst],'分类添加成功！');
    }

    /**
     * 文章分类修改
     *  @return array
     */
    public function classifyEdit($data)
    {
        $data['update_time'] = time();
        $rst =ArticleClassify::where('id',$data['id'])->update($data);
        if ($rst === false) {
            return auto_failure ($data,'分类信息更改失败！');
        } elseif ($rst === 0) {
            return auto_failure ($data,'分类信息未作改动！');
        }

        return auto_return(['number' => $rst],'分类信息更改成功！');
    }

    /**
     * 启用某文章分类
     *  @return array
     */
    public function classifyEnable($data)
    {
        $res = ArticleClassify::where($data)->first();
        if (empty($res)) {
            return auto_failure ($data,'分类id或分类标识不正确！');
        }
        $updateData['status'] = 1;
        $updateData['update_time'] = time();
        $rst = ArticleClassify::where($data)
            ->update($updateData);
        if ($rst === false) {
            return auto_failure ($data,'分类状态更改失败！');
        } elseif ($rst === 0) {
            return auto_failure ($data,'分类状态未发生修改！');
        }

        return auto_return(['number' => $rst], '分类状态修改成功！');
    }


    /**
     * 禁用某文章分类
     * @return array
     */
    public function classifyDisable($data)
    {
        $res = ArticleClassify::where($data)->first();
        if (empty($res)) {
            return auto_failure ($data,'分类id或分类标识不正确！');
        }
        $updateData['status'] = 0;
        $rst = ArticleClassify::where($data)
            ->update($updateData);
        if ($rst === false) {
            return auto_failure ($data,'分类状态更改失败！');
        } elseif ($rst === 0) {
            return auto_failure ($data,'分类状态未发生修改！');
        }

        return auto_return(['number' => $rst], '分类状态修改成功！');
    }

    /**
     * 文章分类删除
     *  @return array
     */
    public function classifyDelete($data)
    {
        $res = ArticleClassify::where($data)->first();
        if (empty($res)) {
            return auto_failure ($data,'分类id或分类标识不正确！');
        }
        $updateData['status'] = -1;
        $updateData['update_time'] = time();
        $rst = ArticleClassify::where($data)
            ->update($updateData);
        if ($rst === false) {
            return auto_failure ($data,'分类状态更改失败！');
        } elseif ($rst === 0) {
            return auto_failure ($data,'分类状态未发生修改！');
        }

        return auto_return(['number' => $rst], '分类状态修改成功！');
    }

    /**
     * 获取文章全部分类
     * @return array
     */
    public function classifyList()
    {
        $manageAppKey =ManageIndex::Api()->getAppKey();
        // $_whereOr = $manageAppKey == '4942b00d6a75eebcce692224b0cb3110' ? true : '`key` = "' . $manageAppKey . '"';
        $query = ArticleClassify::where('status','<>',-1);
        if ( $manageAppKey == '4942b00d6a75eebcce692224b0cb3110') {
            $arr = $query->get()->toArray();
        } else {
            $arr = $query->where('key', $manageAppKey)->get()->toArray();
        }

        foreach ($arr as $k => &$v) {
            $v['status_text'] = $v['status'] == 1 ? '正常' : '禁用';
        }
        $list = list_to_tree($arr,'id','pid','_child', 0);

        return auto_return($list);
    }

    /**
     * 获取某一类文章分类
     */
    public function classifySomeList($data)
    {
        $condition['id'] = $data['pid'];
        $condition['key'] = $data['key'];
        $res = ArticleClassify::where($condition)
            ->first();
        if (empty($res)) {
            return null_data($res,"分类的id和标识不匹配，请重试！");
        }
        $_where['status'] = ['eq',1];
        $_where['pid'] = $data['pid'];
        $arr = ArticleClassify::where($_where)
            ->orderBy('id', 'desc')
            ->select();

        $list = mosaic_child($arr);
        return auto_return($list);
    }


    /**
     * 获不同条件下文章列表获取
     */
    public function classifyCondition($data)
    {
        $arr = ArticleClassify::where('status','eq',1)
            ->where('name','like',"%".$data['keywords']."%")
            ->where('description','like',"%".$data['keywords']."%")
            ->orderBy('id', 'desc')
            ->paginate($data['listRows'], null, null, $data['page'])
            ->toArray();

        return auto_return_page($arr);
    }
}